package nl.hva.three2learn.controller;

import java.io.Reader;
import java.util.List;

import nl.hva.three2learn.authentication.Role;
import nl.hva.three2learn.authentication.UserAuthentication;
import nl.hva.three2learn.dao.LiveSessionDao;
import nl.hva.three2learn.exception.ForbiddenException;
import nl.hva.three2learn.exception.ResourceNotFoundException;
import nl.hva.three2learn.model.LiveSession;
import nl.hva.three2learn.model.User;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
@RequestMapping(value = "/course/live")
public final class LiveSessionController extends BaseController {

	@Autowired
	private LiveSessionDao liveSessionDao;
	
	@RequestMapping(value = "overview", method = RequestMethod.GET)
	public String getOverview(UserAuthentication auth, Model model) {
		
		List<LiveSession> liveSessions = liveSessionDao.findAll();
		
		model.addAttribute("liveSessions", liveSessions);
		
		return "course/live/overview";
	}
	
	@RequestMapping(value = "view/{liveSessionId}", method = RequestMethod.GET)
	public String getView(UserAuthentication auth, Model model,
			@PathVariable("liveSessionId") long liveSessionId) {
		
		LiveSession liveSession = liveSessionDao.findById(liveSessionId);
		
		// Live session wasn't found or the user isn't logged in
		if (liveSession == null || !isUserLoggedIn())
			throw new ResourceNotFoundException();
		
		model.addAttribute("liveSession", liveSession);
		model.addAttribute("user", auth.getUser());
		model.addAttribute("isTeacher", auth.isInRole(Role.TEACHER));
		model.addAttribute("params", "instance=" + liveSession.getTitle() + 
									  "&name=" + auth.getUser().getFirstName() + 
									  "&host=145.92.6.74");
		
		return "course/live/view";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String getCreateForm(UserAuthentication auth) {
		
		// User must be logged in and be a teacher to create a live session
		if (!isUserLoggedIn() || !auth.isInRole(Role.TEACHER))
			throw new ForbiddenException();
		
		return "course/live/create";
	}
		
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(UserAuthentication auth,
			@RequestParam("title") String title) {
		
		User user = auth.getUser();
		
		LiveSession liveSession = new LiveSession();
		liveSession.setTitle(title);
		liveSession.setTeacher(user);
		
		liveSessionDao.save(liveSession);
		
		return "redirect:/course/live/view/" + liveSession.getId();
	}
	
	@RequestMapping(value = "close", method = RequestMethod.POST)
	public String close(@RequestParam("live-session") long liveSessionId) {
		
		LiveSession liveSession = liveSessionDao.findById(liveSessionId);
		liveSessionDao.delete(liveSession);
		
		return "redirect:/";
	}
}
